package cn.com.ghy.android.widget.set;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import cn.com.ghy.android.bean.LoginInfoBean;
import cn.com.ghy.android.exception.DisposeException;
import cn.com.ghy.android.service.LoginOpeService;
import cn.com.ghy.android.util.CommonStatic;
import cn.com.ghy.android.util.CommonUtil;
import cn.com.ghy.android.widget.ComStaticApp;
import cn.com.ghy.android.widget.R;

/**
 * @软件著作权:金红叶纸业集团有限公司
 * @系统名称:SFA系统
 * @Description:修改密码
 * @author wuz  @date 2011-6-2 10:10:49
 * @版本历史:v1.0
 * @Copyright:Copyright (c) 2011-2012 
 */

public class ModifyPasswd extends Activity {

	private EditText txtOldPasswd = null;
	private EditText txtNewPasswd = null;
	private EditText txtNewPasswdVerify = null;
	private Button btnSubmit = null;
	private Bundle bundle = null;
	private ProgressDialog progressDialog = null;

	private String oldPasswd = null;
	private String newPasswd = null;
	private String newPasswdVerify = null;
	private LoginInfoBean loginInfo = null;

	private HttpThread httpThread = null;

	private DisposeException disposeException = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.modify_passwd);
		getSession();
		findView();
		addListener();
	}

	private void getSession() {
		disposeException = ((ComStaticApp) getApplication()).getDisposeException();
		loginInfo = ((ComStaticApp) this.getApplication()).getLoginInfo();
	}

	private void findView() {
		txtOldPasswd = (EditText) this.findViewById(R.id.txtOldPasswd);
		txtOldPasswd.setFilters(CommonUtil.inputFilter);
		txtOldPasswd.setKeyListener(CommonUtil.passwdListener);
		txtNewPasswd = (EditText) this.findViewById(R.id.txtNewPasswd);
		txtNewPasswd.setFilters(CommonUtil.inputFilter);
		txtNewPasswd.setKeyListener(CommonUtil.passwdListener);
		txtNewPasswdVerify = (EditText) this.findViewById(R.id.txtNewPasswdVerify);
		txtNewPasswdVerify.setFilters(CommonUtil.inputFilter);
		txtNewPasswdVerify.setKeyListener(CommonUtil.passwdListener);
		btnSubmit = (Button) this.findViewById(R.id.btnSubmit);
	}

	private void addListener() {
		btnSubmit.setOnClickListener(btnListener);
	}

	private boolean getData() {
		boolean flag = false;
		oldPasswd = txtOldPasswd.getText().toString().trim();
		if (null == oldPasswd || "".equals(oldPasswd)) {
			Toast.makeText(ModifyPasswd.this, "请输入旧密码!", Toast.LENGTH_SHORT).show();
			return flag;
		}
		newPasswd = txtNewPasswd.getText().toString().trim();
		if (null == newPasswd || "".equals(newPasswd)) {
			Toast.makeText(ModifyPasswd.this, "请输入新密码!", Toast.LENGTH_SHORT).show();
			return flag;
		}
		newPasswdVerify = txtNewPasswdVerify.getText().toString().trim();
		if (null == newPasswd || "".equals(newPasswd)) {
			Toast.makeText(ModifyPasswd.this, "请输入确认的新密码!", Toast.LENGTH_SHORT).show();
			return flag;
		}
		if (!newPasswd.equals(newPasswdVerify)) {
			Toast.makeText(ModifyPasswd.this, "两次输入的新密码不一致!", Toast.LENGTH_SHORT).show();
			return flag;
		}
		flag = true;
		return flag;
	}

	private View.OnClickListener btnListener = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
				case R.id.btnSubmit :
					if (!getData()) {
						return;
					}
					httpThread = new HttpThread(handler);
					httpThread.doStart();
					break;
			}

		}
	};

	private class HttpThread extends Thread {
		private Handler handle = null;

		public HttpThread(Handler hander) {
			handle = hander;
		}

		public void doStart() {
			progressDialog = new ProgressDialog(ModifyPasswd.this);
			progressDialog.setTitle("提示信息");
			progressDialog.setMessage("正在请求数据，请稍等......");
			progressDialog.setCancelable(false);
			progressDialog.show();
			this.start();
		}

		public void run() {
			Message message = null;
			try {
				String result = null;
				LoginOpeService service = new LoginOpeService();
				result = service.UserPwdEdit(loginInfo, oldPasswd, newPasswd);
				progressDialog.dismiss();
				message = handle.obtainMessage();
				bundle = new Bundle();
				message.what = CommonStatic.STATE_NORMAL;
				bundle.putString("result", result);
				message.setData(bundle);
				handle.sendMessage(message);
			} catch (Exception ex) {
				progressDialog.dismiss();
				disposeException.handleException(ex);
			}
		}
	}

	private Handler handler = new Handler() {
		public void handleMessage(Message m) {
			switch (m.what) {
				case CommonStatic.STATE_NORMAL :
					String result = m.getData().getString("result");
					if ("Y".equals(result)) {
						new AlertDialog.Builder(ModifyPasswd.this).setTitle("提示信息")
								.setMessage("修改密码成功, 是否关闭此页面?")
								.setPositiveButton("确定", new DialogInterface.OnClickListener() {
									public void onClick(DialogInterface dlg, int sumthin) {
										ModifyPasswd.this.finish();
									}
								}).setNeutralButton("取消", null).show();
					} else {
						new AlertDialog.Builder(ModifyPasswd.this).setTitle("提示信息")
								.setMessage("修改密码失败!").setNeutralButton("关闭", null).show();
					}
					break;
			}
		}
	};
}
